﻿using NFox.Expression.DataSystem;
using NFox.Expression.SymbolValues;
using NFox.Pyeditor.Runtime;
using System.Data;
using System.Windows.Forms;

namespace NFox.Expression.Runtime
{
    public partial class PartCode : UserControl
    {

        ImagePart _part;
        CodeEditor _code;

        public PartCode()
        {
            InitializeComponent();
        }

        public void SetValues(Database database, ImagePart part)
        {
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();

            _part = part;

            if (_code == null)
            {
                _code = new CodeEditor();
                elementHost1.Child = _code;
            }

            if (_part is ParameterizedPart)
            {
                var pp = _part as ParameterizedPart;
                foreach (var name in database.FunctionTable.DictionaryNames)
                    cboBinName.Items.Add(name);
                cboBinName.Items.Add("新的函数库......");
                cboBinName.Text = pp.Function.Index.DictionaryName;
                txtName.Text = pp.Function.Index.Key;

                var func = pp.Function;
                if (func.Data != null)
                {
                    int i = 0;
                    foreach (var parCode in func.ParamNames)
                    {
                        var cell = new DataGridViewTextBoxCell();
                        dataGridView1.Columns.Add(new DataGridViewColumn(cell));
                        dataGridView1.Columns[i++].HeaderText = parCode;
                    }

                    i = 0;
                    foreach (DataRow r in func.Data.Rows)
                    {
                        dataGridView1.Rows.Add(r.ItemArray);
                    }
                }
                _code.Text = func.Code;
            }
            else
            {
                _code.Text = XParser.ToX(_part).ToString();
            }
        }

    }
}
